<?php
namespace YmPhp\Admin\Http\Controllers\Channel\Wechat;

use YmPhp\Admin\Admin;
use YmPhp\Admin\Http\Actions\Wechat\PublishWechatMenu;
use YmPhp\Admin\Models\Channel\WechatMenu;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Layout\Column;
use YmPhp\Admin\Layout\Content;
use YmPhp\Admin\Layout\Row;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;
use YmPhp\Admin\Tree;
use YmPhp\Admin\Widgets\Box;
use YmPhp\Admin\Widgets\Form as WidgetForm;
use YmPhp\Admin\Widgets\Tooltip;

class WechatMenuController extends AdminController
{

    public function index(Content $content)
    {
        return $content->header('商家类目')
            ->body(function (Row $row) {
                $tree = new Tree(new WechatMenu);
                $tree->disableCreateButton();
                $tree->disableQuickCreateButton();
                $tree->disableEditButton();
                $tree->expand(false);
                $tree->tools(function (Tree\Tools $tools) {
                    $tools->add(new PublishWechatMenu());
                });
                $tree->actions(function (Tree\Actions $actions) {
                    if ($actions->getRow()->extension) {
                        $actions->disableDelete();
                    }
                });
                $tree->branch(function ($branch) {
                    Admin::style("
                        .dd-handle{
                            cursor: pointer;
                        }

                    ");
                    //1发送消息，2跳转页面，3调整小程序
                    if ($branch['type'] == 1) {
                        $type = '发送消息';
                    } elseif ($branch['type'] == 2) {
                        $type = '跳转页面';
                    } elseif ($branch['type'] == 3) {
                        $type = '调整小程序';
                    }
                    return " <span style='font-size: 14px;font-weight: bold'><i class='feather icon-align-justify'></i>  {$branch['title']}【{$branch['id']}】 </span> <span style='margin-left: 50px;font-size: 12px'>类型：$type</span>";
                });
                $row->column(7, $tree);
                $row->column(5, function (Column $column) {
                    $form = new WidgetForm();
                    $form->tab(trans('admin.basics'), function ($form) {
                        $form->select('pid',__('wechat.pid'))->options(WechatMenu::selectOptions())->required();
                        $form->text('title',__('wechat.title'))->required();
                        $form->text('sort',__('wechat.sort'))->default(99)->required();
                        $form->radio('type',__('wechat.type'))->options([1=>'发送消息',2=>'跳转页面',3=>'调整小程序'])->default(1)->required();
                        $form->textarea('keyurl',__('wechat.keyurl'))->required();
                    });
                    $form->width(9, 3);
                    $column->append(Box::make(trans('admin.new'), $form));
                });
            });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WechatMenu(), function (Show $show) {
            $show->field('id');
            $show->field('pid');
            $show->field('title');
            $show->field('typd');
            $show->field('keyurl');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WechatMenu(), function (Form $form) {
            $form->select('pid',__('wechat.pid'))->options(WechatMenu::selectOptions())->required();
            $form->text('title',__('wechat.title'))->required();
            $form->text('sort',__('wechat.sort'))->default(99)->required();
            $form->radio('type',__('wechat.type'))->options([1=>'发送消息',2=>'跳转页面',3=>'调整小程序'])->default(1)->required();
            $form->textarea('keyurl',__('wechat.keyurl'))->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
